package com.wwdx.service.impl;

import com.google.common.collect.Maps;
import com.wwdx.model.WwdxBookClassifyModel;
import com.wwdx.service.WwdxBookClassifyServiceI;
import com.wwdx.utils.ConstantHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 * @date 2018/5/17 17:24
 */
@Service
public class WwdxBookClassifyServiceImpl extends BaseServiceImpl<WwdxBookClassifyModel> implements WwdxBookClassifyServiceI{

    @Override
    public Long selectModelCount(WwdxBookClassifyModel model) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String hql = "select count(1) from WwdxBookClassifyModel\t"+getParams(model,params);
        return count(hql,params);
    }

    private String getParams(WwdxBookClassifyModel model, Map<String, Object> params){
        StringBuilder sb = new StringBuilder("where 1=1\n");
        Integer type = model.getType();
        if (type != null){
            sb.append("and type=:type\n");
            params.put("type",type);
        }
        Long pid = model.getPid();
        if (pid == null){
            pid = 0L;
        }
        sb.append("and pid=:pid");
        params.put("pid",pid);
        return sb.toString();
    }

    @Override
    public List<WwdxBookClassifyModel> findBookList(Long id) {
        String key = String.format(ConstantHolder.BOOK_CLASSIFY_LIST, id);
        List<WwdxBookClassifyModel> wwdxBookClassifyModels = (List<WwdxBookClassifyModel>) valueOperations.get(key);
        if (CollectionUtils.isEmpty(wwdxBookClassifyModels)){
            Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
            params.put("id",id);
            String hql = "from WwdxBookClassifyModel where pid=:id and status=0 order by sort desc";
            wwdxBookClassifyModels = this.find(hql, params);
            if (!CollectionUtils.isEmpty(wwdxBookClassifyModels)){
                valueOperations.set(key,wwdxBookClassifyModels);
            }
        }
        return wwdxBookClassifyModels;
    }

    @Override
    public List<WwdxBookClassifyModel> getByBookId(Long bookid) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("bookid",bookid);
        String hql = "from WwdxBookClassifyModel where bookid=:bookid";
        return this.find(hql, params);
    }

    @Override
    public List<WwdxBookClassifyModel> selectModelList(WwdxBookClassifyModel model, Integer page, Integer rows) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String hql = "from WwdxBookClassifyModel\t"+getParams(model,params)+"\torder by sort desc,gmtCreated desc";
        List<WwdxBookClassifyModel> wwdxBookClassifyModels = this.find(hql, params, page, rows);
        Integer type = model.getType();
        if (type == ConstantHolder.Constant.BOOKTYPE){
            wwdxBookClassifyModels.forEach(wwdxBookClassifyModel -> wwdxBookClassifyModel.setName(wwdxBookService.getByBookId(wwdxBookClassifyModel.getBookid()).getBookname()));
        }
        return wwdxBookClassifyModels;
    }
}